/*
 * Serum
 *
 * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
 */

Serum.Form.Module.Wizard = Ext.extend(Ext.Panel, {

    /**
     * Constructor
     *
     * @param   {Object}    config      Redefine items
     */
    constructor: function(config) {
        Serum.Form.Module.Wizard.superclass.constructor.call(this, Ext.apply({
            layout:     'card',
            buttons:    [],
            border:     false,
            activeItem: 'wizard-0',
            bbar: [
                {
                    id: 'move-prev',
                    text: __('base~form_previous'),
                    handler: function(btn) {
                        var name        = this.layout.activeItem.id.split('-');
                        var activeItem  = parseInt(name[1]);
                        this.prePreviousStep(name, activeItem);
                    },
                    scope: this,
                    disabled: true
                },
                '->',
                {
                    id: 'move-next',
                    text: __('base~form_next'),
                    handler: function(btn) {
                        var name        = this.layout.activeItem.id.split('-');
                        var activeItem  = parseInt(name[1]);
                        this.preNextStep(name, activeItem);
                    },
                    scope: this
                }
            ],
            items: []
        }, config));
    },

    /**
     * Preloads form items (delete former items)
     */
    preload: function() {
        this.items.first().preload();
    },

    /**
     * Loads form items
     */
    load: function() {
        this.items.first().load();
    },

    /**
     * Loads the next step
     *
     * @param   {String}    name
     * @param   {Integer}   activeItem
     */
    nextStep: function(name, activeItem) {
        var stepCount = this.items.getCount();

        if ((activeItem+1) < stepCount) {
            this.layout.setActiveItem(name[0] + '-' + parseInt(activeItem+1));
        }
    },

    /**
     * Checks if current step can be validated
     *
     * @param   {String}    name
     * @param   {Integer}   step
     */
    preNextStep: function(name, step) {
        this.getBottomToolbar().items.filter('id', 'move-prev').first().enable();

        var nextStep = this.nextStep(name, step);

        if (nextStep === false) {
            this.getBottomToolbar().items.filter('id', 'move-next').first().disable();
        }
    },

    /**
     * Checks if current step can be validated
     *
     * @param   {String}    name
     * @param   {Integer}   step
     */
    prePreviousStep: function(name, step) {
        this.getBottomToolbar().items.filter('id', 'move-next').first().enable();

        var previousStep = this.previousStep(name, step);

        if (previousStep === false) {
            this.getBottomToolbar().items.filter('id', 'move-prev').first().disable();
        }
    },

    /**
     * Loads the previous step
     *
     * @param   {String}    name
     * @param   {Integer}   step
     */
    previousStep: function(name, activeItem) {
        if (activeItem > 0) {
            this.layout.setActiveItem(name[0] + '-' + parseInt(activeItem-1));
        }
    }
});